<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>漂浮广告</title>
    <style>
        #div1 {
            position: absolute;
            top: 0;
            left: 0;
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>
    <script>
        window.onload = function () {
            var div1 = document.getElementById('div1');
            var x = 3;
            var y = 3;
            function move() {
                if (div1.offsetTop >= window.innerHeight - 200 - 1 || div1.offsetTop < 0) {
                    y = -y;
                }
                if (div1.offsetLeft >= window.innerWidth - 200 - 1 || div1.offsetLeft < 0) {
                    x = -x;
                }
                div1.style.left = div1.offsetLeft + x + 'px';
                div1.style.top = div1.offsetTop + y + 'px';
            }
            setInterval(move, 10);
        }
    </script>
</head>

<body>
    <div id="div1"></div>
</body>

</html>